package panli.app.helloandroid.fragment.listener;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.TextView;

public class DateTextViewClickListener implements OnClickListener {
	private TextView textView;
	private Context context;

	private Calendar calendar;

	public DateTextViewClickListener(Context context, TextView textView,
			Calendar calendar) {
		this.textView = textView;
		this.context = context;
		this.calendar = calendar;
	}

	public DateTextViewClickListener(Context context, TextView textView) {
		this(context, textView, Calendar.getInstance());
	}

	@Override
	public void onClick(View arg0) {
		DatePickerDialog dpDialog = new DatePickerDialog(
				context,
				new OnDateSetListener() {
					@Override
					public void onDateSet(DatePicker arg0, int arg1, int arg2,
							int arg3) {

						calendar.set(arg1, arg2, arg3);
						textView.setText(new SimpleDateFormat("yyyy-MM-dd",
								Locale.getDefault()).format(calendar.getTime()));
					}
				}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH));
		dpDialog.show();
	}

}
